import random
from threading import Thread
import time
from common.utility import Utility

# 主函数
def main(thread_count: int = 5):
    Utility.print_info('entered.')
    threads: list[MyThread] = []
    for i in range(thread_count):
        threads.append(start_thread())
    result_sum: int = 0
    for thread in threads:
        thread.join()
        result_sum += thread.result
    Utility.print_info(f'exited. result sum is {result_sum}.')

class MyThread(Thread):
    def __init__(self, sleep_time: int):
        super().__init__()
        self.sleep_time: int = sleep_time
        self.result: int = sleep_time + int(random.random()*10)

    def run(self):
        Utility.print_info(f'entered. It will sleep {self.sleep_time} seconds.')
        time.sleep(self.sleep_time)
        Utility.print_info('exited.')

# 创建线程
def start_thread() -> MyThread:
    sleep_time: int = int(random.random()*10)
    thread: MyThread = MyThread(sleep_time)
    thread.start()
    return thread


main(5)
